import 'package:get/get.dart';
import 'package:kq_flutter_core_widget/config/kq_core_global.dart';

import '../widgets/image/kq_image.dart';
import '../widgets/imageBox/kq_image_box.dart';

/// 文件预览工具类
class FilePreviewUtil {
  /// 图片预览-单张
  ///
  /// 使用此方法需要在GetX路由注册
  /// ```dart
  /// GetPage(
  ///    name: KqCoreGlobal.routerOfImagePreview,
  ///    page: () => const ImgPreviewPage())
  /// ```
  static previewSingleImg(ImageUrl url) {
    Get.toNamed(KqCoreGlobal.routerOfImagePreview,
        arguments: [url], parameters: {'position': '0'});
  }

  /// 图片预览-多张
  ///
  /// 使用此方法需要在GetX路由注册
  /// ```dart
  /// GetPage(
  ///    name: KqCoreGlobal.routerOfImagePreview,
  ///    page: () => const ImgPreviewPage())
  /// ```
  static previewImg(List<ImageUrl> urls, {int position = 0}) {
    Get.toNamed(KqCoreGlobal.routerOfImagePreview,
        arguments: urls, parameters: {'position': position.toString()});
  }

  /// 文件预览
  ///
  /// 使用此方法需要在GetX路由注册
  /// ```dart
  /// GetPage(
  ///    name: KqCoreGlobal.routerOfFilePreview,
  ///    page: () => const FilePreviewPage())
  /// ```
  ///
  /// 注意：[fileName] 必须是文件的名字，要带后缀名
  /// 如果需要定制预览页面的标题，传[customTitle],否则默认使用[fileName]作为页面标题。
  static previewFile(
      {required String url,
      required String fileName,
      ImageType? imageType,
      String? customTitle}) {
    if (isImage(fileName)) {
      FilePreviewUtil.previewSingleImg(ImageUrl(
          imageType: imageType ??
              (url.startsWith("http") ? ImageType.http : ImageType.file),
          url: url));
      return;
    }
    Get.toNamed(KqCoreGlobal.routerOfFilePreview, arguments: {
      'fileName': fileName,
      'url': url,
      'customTitle': customTitle
    });
  }

  static bool isImage(String fileName) {
    return fileName.toLowerCase().endsWith(".png") ||
        fileName.toLowerCase().endsWith(".jpg") ||
        fileName.toLowerCase().endsWith(".jpeg") ||
        fileName.toLowerCase().endsWith(".gif") ||
        fileName.toLowerCase().endsWith(".webp");
  }
}
